package com.mofofa.maozy.utils.analytics.util

import android.app.Application
import android.os.Bundle
import com.mofofa.maozy.utils.analytics.bean.ParamFacebookS2S
import com.facebook.FacebookSdk
import com.facebook.appevents.AppEventsConstants
import com.facebook.appevents.AppEventsLogger
import com.google.gson.Gson
import com.mofofa.maozy.ProjectApplication
import com.mofofa.maozy.network.ApiUrl
import com.mofofa.maozy.utils.analytics.event.IAnalyticsEvent
import com.mofofa.maozy.utils.analytics.http.HttpHelper
import com.zhi.syc.data.util.ASStoreUtil
import java.math.BigDecimal
import java.math.RoundingMode
import java.util.*

object FacebookUtil {

    private var mAppEventsLogger: AppEventsLogger = AppEventsLogger.newLogger(ProjectApplication.context)

    /**
     * 初始化facebook模块
     *
     * @param context Application
     */
    fun init(context: Application) {
        try {
            FacebookSdk.sdkInitialize(context)
            AppEventsLogger.activateApp(context)
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 保存facebook s2s信息
     */
    fun saveS2SInfo(targetUrlList: List<String>) {
        val params = ParamFacebookS2S()
        val body = Gson().toJson(params).trim()
        println("====== $targetUrlList")

        //传多国时，里触发多次
        val apiPath = ApiUrl.MAOZY_PATH_FACEBOOK_S2S
        targetUrlList.forEach { host ->
            val pathFormat =
                if (host.endsWith(apiPath)) host else (host + apiPath)
            HttpHelper.postData(pathFormat, body)
        }
    }

    /**
     * 打点
     *
     * @param event 事件名
     */
    fun addEvent(event: IAnalyticsEvent) {
        try {
            when (event) {
                IAnalyticsEvent.AT_REGISTER -> {
                    val params = Bundle()
                    params.putString(
                        AppEventsConstants.EVENT_PARAM_REGISTRATION_METHOD,
                        event.name
                    )
                    mAppEventsLogger.logEvent(
                        AppEventsConstants.EVENT_NAME_COMPLETED_REGISTRATION,
                        params
                    )
                }
                IAnalyticsEvent.AT_BASIC -> {
                    val params = Bundle()
                    params.putString(AppEventsConstants.EVENT_PARAM_LEVEL, event.name)
                    mAppEventsLogger.logEvent(
                        AppEventsConstants.EVENT_NAME_ACHIEVED_LEVEL,
                        params
                    )
                }
                IAnalyticsEvent.AT_CONTACT -> {
                    val params = Bundle()
                    params.putString(
                        AppEventsConstants.EVENT_PARAM_CONTENT_TYPE,
                        event.name
                    )
                    params.putString(
                        AppEventsConstants.EVENT_PARAM_SEARCH_STRING,
                        event.name
                    )
                    params.putInt(AppEventsConstants.EVENT_PARAM_SUCCESS, 1)
                    mAppEventsLogger.logEvent(
                        AppEventsConstants.EVENT_NAME_SEARCHED, params)
                }
                IAnalyticsEvent.AT_REV_CARD -> {
                    val params = Bundle()
                    params.putString(
                        AppEventsConstants.EVENT_PARAM_CONTENT_TYPE,
                        event.name
                    )
                    params.putString(
                        AppEventsConstants.EVENT_PARAM_CONTENT_ID,
                        event.name
                    )
                    params.putString(AppEventsConstants.EVENT_PARAM_CURRENCY, "UGX")
                    mAppEventsLogger.logEvent(
                        AppEventsConstants.EVENT_NAME_VIEWED_CONTENT,
                        1.0,
                        params
                    )

                }
                IAnalyticsEvent.AT_LOAN_APPLY -> {
                    val params = Bundle()
                    params.putInt(AppEventsConstants.EVENT_PARAM_NUM_ITEMS, 1)
                    params.putString(
                        AppEventsConstants.EVENT_PARAM_CONTENT_TYPE,
                        event.name
                    )
                    params.putString(
                        AppEventsConstants.EVENT_PARAM_CONTENT_ID,
                        event.name
                    )
                    params.putString(AppEventsConstants.EVENT_PARAM_CURRENCY, "UGX")
                    mAppEventsLogger.logPurchase(
                        BigDecimal.valueOf(1),
                        Currency.getInstance("UGX"),
                        params
                    )

                }
                IAnalyticsEvent.AT_REPAY -> {
                    val params = Bundle()
                    params.putString(
                        AppEventsConstants.EVENT_PARAM_CONTENT_ID,
                        event.name
                    )
                    params.putInt(AppEventsConstants.EVENT_PARAM_SUCCESS, 1)
                    mAppEventsLogger.logEvent(
                        AppEventsConstants.EVENT_NAME_COMPLETED_TUTORIAL,
                        params
                    )
                }
                else -> {}
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 获取总存储大小
     */
    fun getTotalRom(): String {
        var totalRom = ASStoreUtil.getCashTotal(ProjectApplication.instance)
        if (totalRom.endsWith("TB")) {
            totalRom = totalRom.replace("TB".toRegex(), "")
            totalRom =
                BigDecimal(totalRom).multiply(BigDecimal(1024)).stripTrailingZeros().toPlainString()
        } else if (totalRom.endsWith("GB")) {
            totalRom = totalRom.replace("GB".toRegex(), "")
        } else if (totalRom.endsWith("MB")) {
            totalRom = totalRom.replace("MB".toRegex(), "")
            totalRom = BigDecimal(totalRom).divide(BigDecimal(1024), RoundingMode.HALF_UP)
                .stripTrailingZeros().toPlainString()
        }
        return totalRom
    }

    /**
     * 获取可用存储大小
     */
    fun getAvalidableRom(): String {
        var totalRom =
            ASStoreUtil.getCashCanUse(ProjectApplication.instance)
        if (totalRom.endsWith("TB")) {
            totalRom = totalRom.replace("TB".toRegex(), "")
            totalRom =
                BigDecimal(totalRom).multiply(BigDecimal(1024)).stripTrailingZeros().toPlainString()
        } else if (totalRom.endsWith("GB")) {
            totalRom = totalRom.replace("GB".toRegex(), "")
        } else if (totalRom.endsWith("MB")) {
            totalRom = totalRom.replace("MB".toRegex(), "")
            totalRom = BigDecimal(totalRom).divide(BigDecimal(1024), RoundingMode.HALF_UP)
                .stripTrailingZeros().toPlainString()
        }
        return totalRom
    }
}